package edu.gatech.stolen.tea.display.item;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import edu.gatech.stolen.tea.Accounts;
import edu.gatech.stolen.tea.Filter;
import edu.gatech.stolen.tea.ItemEditor;
import edu.gatech.stolen.tea.R;
import edu.gatech.stolen.tea.ToDoItem;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ItemDisplayListActivity extends ListActivity {

	public static final String TAG = "Item Page";
	private Button addButton, changeStatusButton, editButton;
	private ListView listView;
	private String itemName = "";
	private ArrayAdapter<String> itemAdapter;
	private TextView dateTime;
	String dropDownSelection = "";
    private Accounts accounts=new Accounts();
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		Log.v(TAG, "Activity State: onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.itempage);
		accounts=accounts.loadData(getFilesDir().toString());
		
		addButton = (Button) findViewById(R.id.btnAdd);
		changeStatusButton = (Button) findViewById(R.id.btnChangeStatus);
		editButton = (Button) findViewById(R.id.btnEdit);
		dateTime = (TextView) findViewById(R.id.txtDate);

		// spinner and filtering settings
		Spinner spinner = (Spinner) findViewById(R.id.categoryFilter);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.categories_array_filter,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {

				String selection = parent.getItemAtPosition(pos).toString();
				dropDownSelection = selection;
				Toast.makeText(parent.getContext(),
						"The selection is " + selection, Toast.LENGTH_LONG)
						.show();

				if (selection.equals("School")) {

					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_single_choice,
							Filter.byCategory("School",accounts.itemEditor));
					setListAdapter(itemAdapter);
				} else if (selection.equals("Work")) {

					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_single_choice,
							Filter.byCategory("Work",accounts.itemEditor));
					setListAdapter(itemAdapter);
				} else if (selection.equals("Personal")) {

					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_single_choice,
							Filter.byCategory("Personal",accounts.itemEditor));
					setListAdapter(itemAdapter);
				} else if (selection.equals("Due Date")) {

					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_single_choice,
							Filter.byDate(accounts.itemEditor));
					setListAdapter(itemAdapter);
				} else if (selection.equals("Incomplete")) {
					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_single_choice,
							Filter.byCompletion(false,accounts.itemEditor));
					setListAdapter(itemAdapter);

				} else {
					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_single_choice,
							accounts.itemEditor.getItemNames());
					setListAdapter(itemAdapter);
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// Do nothing.
			}
		});

		// viewing the list
		listView = getListView();
		listView.setTextFilterEnabled(true);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				itemName = parent.getItemAtPosition(position).toString();
				;
			}
		});

		addButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				onAddButtonClicked();
			}
		});

		changeStatusButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				onChangeStatusButtonClicked();
			}
		});

		editButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				onEditButtonClicked();
			}
		});

	}

	public void onClick(View view) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
		String dd = dateTime.getText().toString();
		try {
			sdf.parse(dd);
			switch (view.getId()) {
			case R.id.btnUntil:

				itemAdapter = new ArrayAdapter<String>(getContext(),
						android.R.layout.simple_list_item_single_choice,
						Filter.byDueDateBefore(dd,accounts.itemEditor));
				setListAdapter(itemAdapter);
				break;
			case R.id.btnAfter:

				itemAdapter = new ArrayAdapter<String>(getContext(),
						android.R.layout.simple_list_item_single_choice,
						Filter.byDueDateAfter(dd,accounts.itemEditor));
				setListAdapter(itemAdapter);
				break;
			}
		} catch (ParseException e) {
			Log.d("date parse", "unable to parse date "+dd);
			Toast.makeText(getContext(),
					"Please enter a valid date",
					Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
		
	}

	/**
	 * get this context
	 * 
	 * @return this
	 */
	protected Context getContext() {
		// TODO Auto-generated method stub
		return this;
	}

	/**
	 * when addButton is clicked go create an item
	 */
	private void onAddButtonClicked() {

		Log.v(TAG, "Add Button Clicked");
		goToCreateItemPage();
	}

	/**
	 * when edit button is clicked
	 */
	private void onEditButtonClicked() {
		Log.v(TAG, "edit button clicked");
		if (itemName != "") {
			goToMapDisplayPage();
		} else {
			Toast.makeText(getContext(),
					"Please select an item",
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * the change status button toggle the item between complete/incomplete
	 */
	private void onChangeStatusButtonClicked() {

		Log.v(TAG, "Change Status Button Clicked");
		if (itemName != "") {
			ToDoItem item = accounts.itemEditor.getItem(itemName);

			if (item.getStatus() == true) {
				item.setStatus(false);
				Toast.makeText(getContext(),
						"The item " + item.getName() + " is incomplete",
						Toast.LENGTH_LONG).show();
				if (dropDownSelection.equals("Incomplete")) {
					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_1,
							Filter.byCompletion(false,accounts.itemEditor));
					itemAdapter.notifyDataSetChanged();
					setListAdapter(itemAdapter);
				}
			} else {
				item.setStatus(true);
				Toast.makeText(getContext(),
						"The item " + item.getName() + " is complete",
						Toast.LENGTH_LONG).show();
				if (dropDownSelection.equals("Incomplete")) {
					itemAdapter = new ArrayAdapter<String>(getContext(),
							android.R.layout.simple_list_item_1,
							Filter.byCompletion(false,accounts.itemEditor));
					itemAdapter.notifyDataSetChanged();
					setListAdapter(itemAdapter);
				}
			}
		} else {
			Toast.makeText(getContext(),
					"Please select an item",
					Toast.LENGTH_LONG).show();
		}
		accounts.saveData(getFilesDir().toString());
	}

	/**
	 * go to the create item activity
	 */
	protected void goToCreateItemPage() {

		Intent i = new Intent(this, CreateItemActivity.class);
		startActivity(i);

	}

	/**
	 * go to the map display page
	 */
	protected void goToMapDisplayPage() {
		accounts.itemEditor.currentItemName = itemName;
		accounts.saveData(getFilesDir().toString());
		Intent i = new Intent(this, MapDisplayActivity.class);
		startActivity(i);
		

	}

	// /**
	// * returns the name of the current selected item in listview
	// * @return the current selected item name in string
	// */
	// public String getSelectedItemName(){
	// return itemName;
	// }
}
